<template>
	<view class="wrap app_flex app_flex-fdc">
		<view class="tHeader">
			<g-header :rstyle="{width: '174rpx'}" title="库存管理">
				<view class="header-R" @click="onSkipTap('inventoryDetail')">库存明细</view>
			</g-header>
		</view>
		<view class="nav">
			<view class="nav-main">
				<view class="nav-item" :class="{active: navIndex == index}" v-for="(item, index) in navList"
					:key="index" @click="onNavTap(index)">
					{{item.name}}
				</view>
			</view>
		</view>
		<view class="total">
			<view class="total-item" v-for="(item, index) in totalInventory" :key="index">
				<view>{{item.sumAmount || '0'}}/条</view>
				<view v-if="item.unit === '条'">--</view>
				<view v-else>{{item.sumMeter || '0'}}/{{item.unit}}</view>
			</view>
		</view>
		<view class="list" v-if="list.length > 0">
			<view class="itemWrap" v-for="(item, index) in list" :key="index"
				@click="onSkipTap('inventorySingleDetail', index)">
				<view class="item">
					<view class="item-img">
						<image :src="item.cover" mode=""></image>
					</view>
					<view class="item-main">
						<view class="item-title">{{item.productName}}</view>
						<view class="item-mainM">
							<text>色号：{{item.colorName}}</text>
							<text style="margin-left: 14rpx;"
								v-if="item.unit === '条'">{{item.unitsNum + item.unit}}</text>
							<text style="margin-left: 14rpx;" v-else>{{item.unitsNum + item.unit}}/条</text>
						</view>
						<view class="item-mainB">
							<view class="item-mainB_L">
								<text>￥{{item.price}}/{{item.unit}}</text>
								<text>（{{item.emptyDiff}}）</text>
							</view>
							<view class="item-mainB_R">货号：{{item.freightNo}}</view>
						</view>
					</view>
				</view>
				<view class="detail">
					<view class="detail-item">
						<view class="detail-item_L">入库数量：{{item.inAmount}}/条</view>
						<view class="detail-item_R">入库数量：{{item.totalInAmountMeter}}/{{item.unit}}</view>
					</view>
					<view class="detail-item">
						<view class="detail-item_L">销售数量：{{item.outAmount}}/条</view>
						<view class="detail-item_R">销售数量：{{item.totalOutAmountMeter}}/{{item.unit}}</view>
					</view>
					<view class="detail-item">
						<view class="detail-item_L">库存数量：{{item.totalAmount}}/条</view>
						<view class="detail-item_R">库存数量：{{item.totalAmountMeter}}/{{item.unit}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="scanBar">
			<!-- <view class="scanBar-item" @click="onSkipTap('scanCode', 0)">
				<image src="../../static/images/icon-scan-blue1.png"></image>
				<text>扫码入库</text>
			</view> -->
			<view class="scanBar-item" @click="onSkipTap('scanCode', 1)">
				<image src="../../static/images/icon-scan-blue1.png"></image>
				<text>扫码出库</text>
			</view>
		</view>
		<view class="noData app_flex-aicjcc">
			<no-data-v2 v-if="list.length == 0 && hasLoad"></no-data-v2>
		</view>
		<no-more v-if="page > 1 && noMore"></no-more>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				navList: [{
					name: '总库存数量',
					type: 1
				}, {
					name: '入库数量',
					type: 2
				}, {
					name: '出库数量',
					type: 3
				}],
				list: [],
				page: 1,
				size: 15,
				hasLoad: false,
				noMore: false,
				totalInventory: {}
			}
		},
		onLoad(options) {
			this.updatePage()
		},
		onUnload() {
			uni.$off('update-inventory', this.updatePage)
			uni.$off('update-singleUpdate', this.singUpdate)
		},
		onPullDownRefresh() {
			this.updatePage()
		},
		onReachBottom() {
			this.page++
			this.getList()
		},
		methods: {
			onNavTap(index) {
				this.navIndex = index
				this.getTotalInventory()
			},
			updatePage() {
				this.resetPage()
				this.getList()
				this.getTotalInventory()
			},
			resetPage() {
				this.list = []
				this.page = 1
				this.size = 15
				this.hasLoad = false
				this.noMore = false
			},
			onSkipTap(type, index) {
				const urlObj = {
					inventoryDetail: '/pages/inventoryManagement/inventoryDetail',
					inventorySingleDetail: () => {
						uni.$off('update-singleUpdate', this.singUpdate)
						uni.$on('update-singleUpdate', this.singUpdate)
						uni.navigateTo({
							url: `/pages/inventoryManagement/inventorySingleDetail?repoId=${this.list[index].repoId}`
						})
					},
					scanCode: () => {
						uni.$off('update-inventory', this.updatePage)
						uni.$on('update-inventory', this.updatePage)
						uni.navigateTo({
							url: `/pages/scanCode/index?navIndex=${index}`
						})
					}
				}
				this.$g_util.navigationTo(urlObj, type)
			},
			singUpdate(repoId) {
				this.$g_api.repositoryListProduct({
					page: this.page,
					size: this.size
				}).then(({
					data
				}) => {
					if (data.length > 0) {
						data = this.$g_util.concatRequestUrl(data, 'cover')
						// this.list = this.list.concat(data)
						const goodsItem = data.find(item => item.repoId == repoId)
						const goodsIndex = this.list.findIndex(item => item.repoId == repoId)
						if (goodsItem && goodsIndex != -1) {
							this.list.splice(goodsIndex, 1, goodsItem)
						}
					}
				})
				this.getTotalInventory()
			},
			/**
			 * 获取商品列表
			 */
			getList() {
				this.$g_api.repositoryListProduct({
					page: this.page,
					size: this.size
				}).then(({
					data
				}) => {
					uni.stopPullDownRefresh()
					this.hasLoad = true
					if (data.length > 0) {
						data = this.$g_util.concatRequestUrl(data, 'cover')
						this.list = this.list.concat(data)
					} else {
						this.noMore = true
					}
				})
			},
			/**
			 * 获取总库存量
			 */
			async getTotalInventory() {
				this.$g_api.repositoryStatistics({
					queryType: this.navList[this.navIndex].type
				}).then(({
					data
				}) => {
					console.log(data);
					this.totalInventory = data
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.wrap {
		min-height: 100vh;
		padding-top: calc(var(--status-bar-height) + 98rpx);
		padding-bottom: calc(constant(safe-area-inset-bottom) + 120rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
	}

	.tHeader {
		position: fixed;
		top: 0;
		left: 0;
		width: 750rpx;
		z-index: 9999;
	}

	.header-R {
		color: $g-main-color;
	}

	.nav {
		display: flex;
		justify-content: center;
		padding-top: 30rpx;
		background-color: #fff;
	}

	.nav-main {
		display: flex;
		height: 70rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.nav-item {
		width: 200rpx;
		line-height: 70rpx;
		text-align: center;
		border: 1px solid $g-border-color;
		box-sizing: border-box;

		&.active {
			color: $g-main-color;
			border: 1px solid $g-main-color;
			background-color: #E5EEFF;
		}

		&:nth-child(1) {
			border-radius: 10rpx 0 0 10rpx;
		}

		&:nth-child(2) {}

		&:nth-child(3) {
			border-radius: 0 10rpx 10rpx 0;
		}
	}

	.total {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		padding: 30rpx 30rpx 20rpx;
		background-color: #fff;
	}

	.total-item {
		width: 340rpx;
		height: 110rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin-bottom: 10rpx;
		border-radius: 10rpx;
		background-color: $g-main-bg;
	}

	.list {
		margin-top: 20rpx;
	}

	.itemWrap {
		padding: 30rpx 20rpx;
		border-bottom: 1px solid $g-border-color;
		background-color: #fff;
	}

	.item {
		display: flex;
		padding-bottom: 30rpx;
	}

	.item-img {
		width: 120rpx;
		font-size: 0;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 10rpx;
			background-color: $g-main-bg;
		}
	}

	.item-main {
		flex: 1;
		margin-left: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.item-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.item-mainM {
		font-size: 24rpx;
		color: #999;
	}

	.item-mainB {
		display: flex;
		justify-content: space-between;
	}

	.item-mainB_L {
		text {
			&:first-child {
				color: #FF4C4C;
				font-size: 26rpx;
			}

			&:last-child {
				color: #999999;
				font-size: 20rpx;
			}
		}
	}

	.item-mainB_R {
		font-size: 24rpx;
		color: #999;
	}

	.detail {
		padding: 20rpx;
		border-radius: 10rpx;
		background-color: $g-main-bg;
	}

	.detail-item {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;

		&:last-child {
			margin-bottom: 0;
		}
	}

	.detail-item_L {}

	.detail-item_R {}

	.scanBar {
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		// justify-content: center;
		width: 750rpx;
		height: 100rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box;
		background: #FFFFFF;
		box-shadow: 0 0 10rpx 0 rgba(0, 0, 0, 0.1);
	}

	.scanBar-item {
		position: relative;
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 0;

		image {
			width: 38rpx;
			height: 38rpx;
		}

		text {
			margin-left: 10rpx;
			line-height: 38rpx;
			font-size: 32rpx;
			color: $g-main-color;
		}

		&:first-child {
			&::after {
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
				content: '';
				height: 60rpx;
				width: 2rpx;
				border-radius: 2rpx;
				background-color: #e6e6e6;
			}
		}
	}

	.noData {
		flex: 1;
		position: relative;
	}
</style>
